package com.lxj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lxj.constant.MenuConstant;
import com.lxj.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxj.mapper.SysMenuMapper;
import com.lxj.domain.SysMenu;
import com.lxj.service.SysMenuService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author lxj
 * @date 2021/7/31 9:39
 * @Description: 
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SysMenuMapper SysMenuMapper;

    /**
     * 查询当前用户的树菜单
     * 1.查缓存
     * 2.缓存没有查询数据库
     * 3.缓存有直接拿到
     * 4.组装树菜单
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> loadTreeByUserId(String userId) {
        log.info("查询用户{}，的菜单", userId);
        String menuStr = redisTemplate.opsForValue().get(MenuConstant.MENU_PREFIX + userId);
        List<SysMenu> sysMenus = null;
        // 缓存有直接拿到
        if(StringUtils.hasText(menuStr)){
            sysMenus = JSON.parseArray(menuStr,SysMenu.class);
        }else {
            //缓存没有查询数据库
            sysMenus = SysMenuMapper.findUserMenu(userId);
            if(!CollectionUtils.isEmpty(sysMenus)){
                //不为空就存入redis
                redisTemplate.opsForValue().set(MenuConstant.MENU_PREFIX+userId,JSON.toJSONString(sysMenus), Duration.ofDays(7));
            }
        }
        // 空的就直接返回
        if (CollectionUtils.isEmpty(sysMenus)) {
            return Collections.emptyList();
        }
        // 组装树菜单
        return translateTree(sysMenus,0L);
    }


    /**
     * 组装树菜单的方法
     *
     * @param sysMenus
     * @param pid
     * @return
     */
    private List<SysMenu> translateTree(List<SysMenu> sysMenus, long pid) {

        /*//先拿到最外面的跟菜单
        List<SysMenu> root = sysMenus.stream()
                .filter(sysMenu -> sysMenu.getParentId()
                        .equals(pid))
                .collect(Collectors.toList());
        // 循环根节点
        root.forEach(r -> {
            // 循环所有菜单  找到谁的父节点是外面的id 即可
            List<SysMenu> child = sysMenus.stream()
                    .filter(sysMenu -> sysMenu.getParentId()
                            .equals(r.getMenuId()))
                    .collect(Collectors.toList());
            r.setList(child);
        });*/

        // 递归加载
        List<SysMenu> root = sysMenus.stream()
                .filter(sysMenu -> sysMenu.getParentId().equals(pid))
                .collect(Collectors.toList());
        root.forEach(r -> r.setList(translateTree(sysMenus,r.getMenuId())));
        return root;
    }
}
